package com.xb.dao;

import com.xb.entity.Article;
import com.xb.entity.User;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;

public class ArticleDao extends BaseDao{


    /**
     * 文章搜索+分页
     * @param title
     * @param startIndex
     * @param pageSize
     * @return
     */
    public List<Article> findPage(String title, Integer startIndex, Integer pageSize) {
        String sql="select * from article where title like ? or content like? order by publish_date desc limit ?,?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Article.class),"%"+title+"%","%"+title+"%",startIndex,pageSize);
    }


    /**
     *
     * @param title
     * @return
     */
    public Integer countByTitle(String title) {
        String sql="select count(*) from article where title like ? or content like?";
        return jdbcTemplate.queryForObject(sql, Integer.class,"%"+title+"%","%"+title+"%");

    }


    /**
     * 发布文章
     * @param article
     */
    public void save(Article article) {
        jdbcTemplate.update("insert into article values(null,?,?,?,?,?,?)",
                article.getTitle(),
                article.getContent(),
                article.getBrowseCount(),
                article.getPublishDate(),
                article.getPublishRealName(),
                article.getUserId()
        );
    }


    /**
     * 我的收藏+搜索+分页
     * @param title
     * @param startIndex
     * @param pageSize
     * @param userId
     * @return
     */
    public List<Article> findFavoritePage(String title, Long userId, Integer startIndex, Integer pageSize) {
        String sql="select * from article where id in (select  a_id from favorite where u_id=?) and title like ?  limit ?,?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Article.class),userId,"%"+title+"%",startIndex,pageSize);
    }


    /**
     * @param title
     * @param id
     * @return
     */
    public Integer countFavoirtePage(String title, Long id) {
        String sql="select count(*) from article where id in (select  a_id from favorite where u_id=?) and title like ? ";
        return jdbcTemplate.queryForObject(sql, Integer.class,id,"%"+title+"%");

    }


    /**
     * 更新浏览量
     * @param articleId
     */
    public void updateBrowseCount(Long articleId) {
        String sql="update article set browse_count=browse_count+1 where id=?";
        jdbcTemplate.update(sql,articleId);

    }


    /**
     * 文章详情
     * @param articleId
     * @return
     */
    public Article findById(Long articleId) {
        String sql="select * from article where id=?";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(Article.class),articleId);
    }


    /**
     * 收藏文章次数
     * @param articleId
     * @return
     */
    public Integer findFavoriteCount(Long articleId) {
        String sql="select count(*) from favorite where a_id=?";
        return jdbcTemplate.queryForObject(sql, Integer.class,articleId);

    }


    /**
     * 我是否收藏过该文章
     * @param id
     * @param articleId
     * @return
     */
    public int findFavoriteId(Long id, Long articleId) {
        String sql="select count(*) from favorite where  u_id=? and  a_id=?";
        return jdbcTemplate.queryForObject(sql, Integer.class,id,articleId);

    }


    /**
     * 我关注的人，并且他们也收藏了这篇文章
     * @param id
     * @param articleId
     * @return
     */
    public List<User> findUserList(Long id, Long articleId) {
        String sql="SELECT * FROM user WHERE id in (select user_focus_id from userfocus where user_id=? and user_focus_id in (select  u_id from favorite where a_id=?))";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(User.class), id,articleId);

    }

    /**
     * 我是来取消文章的
     * @param id
     * @param articleId
     */
    public void delFavorite(Long id, Long articleId) {
        String sql="delete from favorite where  u_id=? and  a_id=?";
         jdbcTemplate.update(sql,id,articleId);
    }

    /**
     * 我是来收藏文章的
     * @param id
     * @param articleId
     */
    public void addFavorite(Long id, Long articleId) {
        String sql="insert into favorite values (?,?)";
        jdbcTemplate.update(sql,id,articleId);
    }
}
